package crp.core.workflow.domain.services;

import com.qwlabs.panache.Where;
import com.qwlabs.cdi.Primary;
import crp.core.workflow.domain.models.WorkflowTaskQuery;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.util.Objects;

@Primary
@ApplicationScoped
public class WorkflowQueryConditionBuilderCombiner implements WorkflowQueryConditionBuilder {

    private final Instance<WorkflowQueryConditionBuilder> workflowQueryConditionBuilders;

    @Inject
    public WorkflowQueryConditionBuilderCombiner(Instance<WorkflowQueryConditionBuilder> workflowQueryConditionBuilders) {
        this.workflowQueryConditionBuilders = workflowQueryConditionBuilders;
    }

    @Override
    public Boolean support(String topic) {
        return null;
    }

    @Override
    public Where build(WorkflowTaskQuery workflowTaskQuery) {
        return workflowQueryConditionBuilders.stream().filter(Objects::nonNull)
                .filter(item -> item.support(workflowTaskQuery.getWorkflowTopic()))
                .findFirst().get().build(workflowTaskQuery);
    }
}
